import time
import unittest
import sqlparse.exceptions
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver import Keys
from data import db
from ddt import ddt,file_data


sql=db(user="root",password="yidong666",host="10.59.9.12",database="a3mall")

class A3mall(unittest.TestCase):

    def setUp(self) -> None:
        self.driver=webdriver.Firefox()
        self.driver.get('http://10.59.9.12:7001/')
        self.driver.maximize_window()
        time.sleep(4)

    def tearDown(self) -> None:
        time.sleep(3)
        self.driver.quit()

    @file_data(r'D:\gao1\PythonProject4\jsona.json')
    def test_a3mall_01(self,username,password):
        #点击登录
        self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        self.driver.implicitly_wait(10)
        #输入账号和密码
        self.driver.find_element(By.NAME,'username').send_keys('13811033962')
        self.driver.find_element(By.NAME,'password').send_keys('123456')
        #等待时间输入验证码
        time.sleep(15)
        #点击登录按钮
        self.driver.find_element(By.XPATH,'//*[@id="theForm"]/div[5]/a').click()
        time.sleep(3)
        #断言1 登陆成功：欢迎回来
        dy1=self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[1]').text
        print("dy1:",dy1)
        #数据库断言（用户余额）replace方法，把 “¥” 替换成空字符（比如 “¥ 988389770.25” 变成 “988389770.25”）
        dy2=self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
        print(dy2.replace('¥ ',''))
        print('断言2：',dy2)
        #select amount from mall_users where username = "13811033962"
        ye=sql.select('select amount from mall_users where username = "13811033962" ')
        print(ye[0][0])
        time.sleep(3)
        # 数据库断言（积分）
        dy3 = self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[2]/span[1]').text
        print(dy3.replace('积分 ', ''))
        print('断言3：', dy3)
        # select amount from mall_users where username = "13811033962"
        ye = sql.select('select amount from mall_users where username = "13811033962" ')
        print(ye[0][0])
        time.sleep(3)
        #点击首页跳转至首页页面
        self.driver.find_element(By.XPATH,'/html/body/div[3]/div[2]/ul/li[1]/a').click()
        time.sleep(3)